<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/6/17
 * Time: 17:39
 */

namespace app\manage\controller;


use app\manage\model\Token;
use think\Db;
use think\Validate;

class User
{

    public function __construct()
    {
        $this->token=new Token();
        $this->user=new \app\manage\model\User();
    }

    /**
     * 用户管理-会员列表
     */
    public function userList(){
        $param=input();
        $user_id = get_header_id("token");
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];

        $returnData=$this->user->userList($param,$page,$page_size);

        returnJson(200,'success',$returnData);

    }

    /**
     * 用户管理-会员详情
     */
    public function userDetail(){
        $member_id=input('member_id');
        $returnData=$this->user->userDetail($member_id);

        returnJson(200,'success',$returnData);
    }

    /**
     * 用户管理-修改会员等级--页面
     */
    public function userLevel(){
        $member_id=input('member_id');
        $returnData=$this->user->editUserLevel($member_id,1);

        returnJson(200,'success',$returnData);
    }

    /**
     * 用户管理-修改会员等级提交
     */
    public function userLevelSubmit(){
        $param=input();
        $validate = new Validate([
            ['member_id', 'require', '用户id不能为空'],
            ['grade_type', 'require', '会员等级不能为空'],
            ['end_time', 'require', '会员到期时间不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $this->user->editUserPost($param,1);

        returnJson(200,'操作成功');
    }

    /**
     * 用户管理-修改会员积分--页面
     */
    public function userScore(){
        $member_id=input('member_id');
        $returnData=$this->user->editUserLevel($member_id,2);

        returnJson(200,'success',$returnData);
    }

    /**
     * 用户管理-修改会员积分-提交
     */
    public function userScoreSubmit(){
        $param=input();
        $validate = new Validate([
            ['member_id', 'require', '用户id不能为空'],
            ['score', 'require', '会员积分不能为空'],
        ]);
        if(!$validate->check($param))
        {
            returnJson(-2003,$validate->getError());
        }
        $this->user->editUserPost($param,2);

        returnJson(200,'操作成功');
    }

    /**
     * 查询用户的地址   会员列表--搜索
     */
    public function addressList(){
        $returnData=$this->user->selectUserAddress();
        returnJson(200,'success',$returnData);
    }


    /**
     * 会员升级消耗积分记录--列表
     */
    public function consumeScoreList(){
        $param=input();
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];

        $returnData=$this->user->scoreList($param,$page,$page_size);

        returnJson(200,'success',$returnData);
    }

    /**
     * 会员转账记录--列表
     */
    public function transferList(){
        $param=input();
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];

        $returnData=$this->user->transferList($param,$page,$page_size);

        returnJson(200,'success',$returnData);
    }

    /**
     * 推荐关系
     */
    public function relationship(){
        //$returnData=$this->user->relationship();

        $param=input();
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];

        $returnData=$this->user->relation($page,$page_size);

        returnJson(200,'success',$returnData);

    }

    /**
     * 查询下级关系
     */
    public function lowerShip(){
        $param=input();
        $member_id=$param['member_id'];
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];
        $returnData=$this->user->lowerShip($member_id,$page,$page_size);

        returnJson(200,'success',$returnData);
    }

    /**
     * 推客列表
     */
    public function twitterList(){
        $param=input();
        $page=empty($param['current_page']) ? 1 : $param['current_page'];
        $page_size=empty($param['page_num']) ? 10: $param['page_num'];

        $returnData=$this->user->twitterList($param,$page,$page_size);

        returnJson(200,'success',$returnData);
    }

    public function test(){


    }

    public function test2(){
        $returnData=$this->getLists(0);
        returnJson(200,'success',$returnData);
    }

    function getLists($pid = 0, &$lists = array(), $deep = 1) {
        $row=Db::name('member')
            ->field('member_id as id,mobile,nickName')
            ->where(['uid_1'=>$pid])
            ->select();
        //$lists[] = $row;
        foreach ($row as $k=>&$v){
            //$child=$this->getLists($v['id'], $lists, ++$deep);
            $child=$this->getLists($v['id'], $row, ++$deep);
            $v['child']=$child;
        }

        return $row;
    }


}